package com.shenzhen.horselive.wx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.util.Log;

import com.google.gson.Gson;
import com.shenzhen.horselive.Constants;
import com.shenzhen.horselive.beans.WxPayRequestBean;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.File;


public class WXManager {
    private static final String TAG = "WXManager";
    private static final String APP_ID = Constants.WECHAT_APPID;

    /**
     * 分享图片缩略图大小
     */
    private static final int THUMB_SIZE = 150;

    private static volatile WXManager instance;
    private Context context;
    // IWXAPI 是第三方app和微信通信的openApi接口
    private IWXAPI api;

    private WXManager() {
    }

    public void init(Context context) {
        this.context = context.getApplicationContext();
        regToWx();
    }

    public static WXManager getInstance() {
        if (instance == null) {
            synchronized (WXManager.class) {
                if (instance == null) {
                    instance = new WXManager();
                }
            }
        }

        return instance;
    }

    /**
     * 注册到微信
     */
    private void regToWx() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(context, APP_ID, true);

        // 将应用的appId注册到微信
        api.registerApp(APP_ID);

        // 建议动态监听微信启动广播进行注册到微信
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                // 将该app注册到微信
                api.registerApp(APP_ID);
            }
        }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
    }

    /**
     * 分享图片
     * @param picPath
     */
    public void sharePicture(String picPath) {
        File file = new File(picPath);
        if (!file.exists()) {
            Log.e(TAG, "share picture but " + picPath + " is not exist.");
            return;
        }

        //初始化 WXImageObject 和 WXMediaMessage 对象
        WXImageObject imgObj = new WXImageObject();
        imgObj.setImagePath(picPath);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        //设置缩略图,暂时取消
//        Bitmap thumbBmp = BitmapFactory.decodeFile(picPath);
//        msg.thumbData = WXUtils.bmpToByteArray(thumbBmp, true);

        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = WXUtils.buildTransaction("img");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;

        //调用api接口，发送数据到微信
        api.sendReq(req);
    }




    /**
     * 分享图片到对话
     * @param
     */
    public void sharePictureToDuihua(Bitmap bitmap) {
//        File file = new File(picPath);
//        if (!file.exists()) {
//            Log.e(TAG, "share picture but " + picPath + " is not exist.");
//            return;
//        }

        //初始化 WXImageObject 和 WXMediaMessage 对象
        WXImageObject imgObj = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        //设置缩略图,暂时取消
//        Bitmap thumbBmp = BitmapFactory.decodeFile(picPath);
//        msg.thumbData = WXUtils.bmpToByteArray(thumbBmp, true);

        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = WXUtils.buildTransaction("img");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        //调用api接口，发送数据到微信
        api.sendReq(req);
    }




    /**
     * 分享图片到朋友圈
     * @param
     */
    public void sharePictureToQuan(Bitmap bitmap) {
//        File file = new File(picPath);
//        if (!file.exists()) {
//            Log.e(TAG, "share picture but " + picPath + " is not exist.");
//            return;
//        }

        //初始化 WXImageObject 和 WXMediaMessage 对象
        WXImageObject imgObj = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        //设置缩略图,暂时取消
//        Bitmap thumbBmp = BitmapFactory.decodeFile(picPath);
//        msg.thumbData = WXUtils.bmpToByteArray(thumbBmp, true);

        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = WXUtils.buildTransaction("img");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        //调用api接口，发送数据到微信
        api.sendReq(req);
    }



    //微信支付
    public  void   goToPay(String  json
    )
    {
        Gson  gson = new Gson();
        try {
            WxPayRequestBean  requestBean =  gson.fromJson(json,WxPayRequestBean.class);
            PayReq request = new PayReq();
            request.appId = requestBean.getAppid();
            request.partnerId = requestBean.getPartnerid();
            request.prepayId= requestBean.getPrepayid();
            request.packageValue = "Sign=WXPay";
            request.nonceStr= requestBean.getNoncestr();
            request.timeStamp= requestBean.getTimestamp();
            request.sign= requestBean.getSign();
            api.sendReq(request);
        }catch (Exception e)
        {

        }

    }


    //微信支付
    public  void   goToPay(WxPayRequestBean  requestBean
    )
    {

        try {
            PayReq request = new PayReq();
            request.appId = requestBean.getAppid();
            request.partnerId = requestBean.getPartnerid();
            request.prepayId= requestBean.getPrepayid();
            request.packageValue = "Sign=WXPay";
            request.nonceStr= requestBean.getNoncestr();
            request.timeStamp= requestBean.getTimestamp();
            request.sign= requestBean.getSign();
            api.sendReq(request);
        }catch (Exception e)
        {
        }

    }


    public IWXAPI getWXAPI() {
        return api;
    }


}
